package com.fumihiko.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class Md5Util {

	/**
	 * 
	 * @param srcSignString
	 * @param key
	 * @param charset
	 * @return
	 */
	public static String getMd5(String srcSignString, String key, String charset) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(srcSignString.getBytes(charset));

			String result = "";
			byte[] temp;
			temp = md5.digest(key.getBytes(charset));
			for (int i = 0; i < temp.length; i++) {
				result += Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
			}

			return result;

		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * hamc-md5
	 * @param encodingCharset 字符编码
	 * @param aValue 源串
	 * @param aKey 秘钥
	 * @return
	 */
	public static String hmacSign(String encodingCharset, String aValue, String aKey) {
		byte k_ipad[] = new byte[64];
		byte k_opad[] = new byte[64];
		byte keyb[];
		byte value[];
		try {
			keyb = aKey.getBytes(encodingCharset);
			value = aValue.getBytes(encodingCharset);
		} catch (UnsupportedEncodingException e) {
			keyb = aKey.getBytes();
			value = aValue.getBytes();
		}
		Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
		Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
		for (int i = 0; i < keyb.length; i++) {
			k_ipad[i] = (byte) (keyb[i] ^ 0x36);
			k_opad[i] = (byte) (keyb[i] ^ 0x5c);
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		md.update(k_ipad);
		md.update(value);
		byte dg[] = md.digest();
		md.reset();
		md.update(k_opad);
		md.update(dg, 0, 16);
		dg = md.digest();
		
		String result = "";
		for (int i = 0; i < dg.length; i++) {
			result += Integer.toHexString((0x000000ff & dg[i]) | 0xffffff00).substring(6);
		}
		return result;
	}
	
	/**
	 * 标准md5散列
	 * @param data 源数据
	 * @return
	 */
	public static String md5Hash(String data) {
		String result = "";
		try {
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(data.getBytes("UTF-8"));
			byte[] md = mdInst.digest();
			for (int i = 0; i < md.length; i++) {
				result += Integer.toHexString((0x000000ff & md[i]) | 0xffffff00).substring(6);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public static String md5(String plainText) throws NoSuchAlgorithmException{
		MessageDigest md = MessageDigest.getInstance("MD5"); 
		md.update(plainText.getBytes()); 
		byte b[] = md.digest(); 
		int i; 
		StringBuffer buf = new StringBuffer(""); 
		for (int offset = 0; offset < b.length; offset++) { 
		i = b[offset]; 
		if(i<0) i+= 256; 
		if(i<16) 
		buf.append("0"); 
		buf.append(Integer.toHexString(i)); 
		} 
		return buf.toString();
	}
	
	public static void main(String[] a){
		System.out.println(md5Hash("100000abcd885e657b0b64070fadd61925247cfd8"));
		System.out.println(getMd5("sx@123","14322","UTF-8"));
	}
}
